<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="4">
            <el-form-item label="物品名称：" prop="consumeName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入物品名称" v-model="queryParams.consumeName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="规格型号：" prop="ggType" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入规格型号" @keyup.enter="handleQuery"
                v-model="queryParams.ggType" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="领用人：" prop="createUserId" label-width="80">
              <el-select clearable v-model="queryParams.createUserId">
                <el-option v-for="dict in proxy.addListNoLimit(
                  userList,
                  'userId',
                  'nickName'
                )" :key="dict.userId" :label="dict.nickName" :value="dict.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="领用日期：" prop="expirationType" label-width="80">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状态：">
              <el-select clearable v-model="queryParams.degree">
                <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-row justify="end">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">领用耗材用品列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="padding-bottom: 5px" justify="end">
          <el-button link icon="FolderChecked" type="success" @click="exportDateFormPdf"
            >导出领用记录</el-button
          >
        </el-row>
        <el-table ref="mainTableRef" :data="useTable" style="margin-top: 5px" @selection-change="handleSelChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="物品名称" prop="consume.consumeName" align="center" />
          <el-table-column label="规格型号" prop="consume.ggType" align="center" />
          <el-table-column label="供货商" prop="consume.supplyName" align="center" />
          <el-table-column label="生产厂商" prop="consume.produceName" align="center" />
          <el-table-column label="批号" prop="consume.produceCode" align="center" />
          <el-table-column label="保存位置" prop="consume.saveLocation" align="center" />
          <el-table-column label="领用人" prop="createUserId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.createUserId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="领用日期" prop="outDate" align="center" />
          <el-table-column label="领用数量" prop="outCount" align="center">
            <template #default="scope">
              <span style="color: var(--theme-deep-color)"> {{ scope.row.outCount }}</span>
              <!-- <el-button link @click="jcxmCountClick(scope.row.ybsjId)">{{
                }}</el-button> -->
            </template>
          </el-table-column>
          <el-table-column label="用途" prop="remark" align="center" />
          <el-table-column label="状态" prop="stepId" align="center" />
          <el-table-column label="操作" align="center" fixed="right">
            <template #default="scope">
              <el-button link type="success">领用人签字</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="notvalueUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzRecord">
import { listUseList, } from "@/api/resource/hcyp";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();

const useTable = ref([]);
const multiplexTable = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    createUserId: -1,
  },
});

const { queryParams } = toRefs(data);

const dateRange = ref([]);
const statusList = ref([{ label: "不限", value: 1 }]);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
function getList() {
  let temp = {};
  temp = {
    "consume.consumeName": queryParams.value.consumeName,
    "consume.ggType": queryParams.value.ggType,
    createUserId: queryParams.value.createUserId,
    pageSize: queryParams.value.pageSize,
    pageNum: queryParams.value.pageNum,
  };
  listUseList(proxy.addDateRange(temp, dateRange.value)).then((res) => {
    useTable.value = res.rows;
    total.value = res.total;
  });
}

getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// function jcxmCountClick(row) {
//   bzwzList({ bzwzId: row }).then((res) => {
//     multiplexTable.value = res.data;
//     jcxmOpen.value = true;
//   });
// }

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

function exportDateFormPdf() {
  if(dateRange.value.length == 0){
    proxy.$modal.msgError("请选择领用时间！");
    return;
  }
  proxy.addDateRange(queryParams.value, dateRange.value)
  if(queryParams.value.params){
    Object.assign(queryParams.value, queryParams.value.params)
    delete queryParams.value.params;
  }
  const params = {modelType: 22,params:queryParams.value};
  proxy.download("/flowable/task/exportReportPdf", params, `耗材用品领用记录.pdf`);
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
